import { OpenAIEmbeddings } from '@langchain/openai';
import { config } from '../config/config';

export class EmbeddingService {
  private embeddings: OpenAIEmbeddings;

  constructor() {
    this.embeddings = new OpenAIEmbeddings({
      openAIApiKey: config.dashscope.apiKey,
      configuration: {
        baseURL: config.dashscope.baseURL
      },
      modelName: 'text-embedding-v2'
    });
  }

  async embedText(text: string) {
    try {
      return await this.embeddings.embedQuery(text);
    } catch (error) {
      console.error('Error embedding text:', error);
      throw error;
    }
  }

  async embedTexts(texts: string[]) {
    try {
      return await this.embeddings.embedDocuments(texts);
    } catch (error) {
      console.error('Error embedding texts:', error);
      throw error;
    }
  }
}
